import {ajax} from "@/utils/ajax.js"

export const fetchMyList = ({startTime,pageSize=10})=>{
    return ajax({
		url:'/imSession/myList',
		data:{
			startTime,
            pageSize
		}
	})
}

export const setTop = ({id,status=false}) =>{
	return ajax({
		url:'/imSession/top',
		data:{
			id,
			status:status?1:0
		}
	})
}


export const setSilent = ({id,status=false}) =>{
	return ajax({
		url:'/imSession/noDisturb',
		data:{
			id,
			status:status?1:0
		}
	})
}


export const removeSession = (id,type) =>{
	return ajax({
		url:'/imSession/byObjDel',
		data:{
			toId:id,
			toObj:type
		}
	})
}


export const fetchOne = (id) =>{
	return ajax({
		url:'/imSession/byIdDetail',
		data:{
			id
		}
	})
}


export const fetchOneByUser = (uid) =>{
	return ajax({
		url:'/imSession/byUserIdDetail',
		data:{
			id:uid
		}
	})
}


export const fetchOneByGroup = (gid) =>{
	return ajax({
		url:'/imSession/byGroupIdDetail',
		data:{
			id:gid
		}
	})
}

export const activeBySid = (sid)=>{
	return ajax({
		url:'/imSession/openSessionId',
		data:{
			id:sid
		}
	})
}

export const activeByTo = (id,type)=>{
	return ajax({
		url:'/imSession/openSession',
		data:{
			toId:id,
			toObj:type
		}
	})
}

export const clearHistory = (id,type)=>{
	return ajax({
		url:'/imSession/cleanMsg',
		data:{
			toId:id,
			toObj:type
		}
	})
}

export const searchHistory = async ({
	id,
	chatType,
	msgType,
	keyword,
	startTime,
	endTime,
	order = 'desc',
	pageNo=1,
	pageSize=10
})=>{
	const list = await ajax({
		url:'/imMessageLog/search',
		data:{
			toBizId:id,
			toBizType:chatType,
			msgType,
			keyword,
			startTime,
			endTime,
			pageNo,
			pageSize,
			order
		}
	})
	return list.map(({extras,...rest})=>({...rest,extras:JSON.parse(extras||'{}')}))
}
